﻿using System;
using System.Threading.Tasks;

using Linq2Crm.ObjectModel.DomainObject;

namespace Linq2Crm.Request
{
	public sealed class UpdateRequest : RequestBase
	{
		private UpdateRequest(Type type) : base(type)
		{
		}

		public IDomainObject Entity { get; set; }

		public Action<UpdateResponse> Callback { get; set; }

		public static UpdateRequest Create<T>(IDomainObject entity)
			where T : IDomainObject
		{
         if (entity == null)
         {
            throw new ArgumentNullException();
         }

			return new UpdateRequest(typeof (T))
			       	{
			       		Query = CreateQuery<T>(),
			       		Entity = entity
			       	};
		}

		internal override void Execute()
		{
			DomainObjectContext.Instance.ExecuteUpdateRequest(this);
		}

		internal override async Task<ResponseBase> ExecuteAsync()
		{
			var response = await DomainObjectContext.Instance.ExecuteUpdateRequestAsync(this);
			return response;
		}
	}
}